# -*- coding: utf-8 -*-

"""
@Time        : 2020/9/11
@Author      : 你猜
@File        : utils
@Description : 
"""
import hashlib
import uuid
import os
from functools import wraps
from django.shortcuts import redirect


def encryption_md5(value):
    """
    使用md5加密
    """

    my_md5 = hashlib.md5()
    my_md5.update(value.encode("utf-8"))
    return my_md5.hexdigest()


def do_file_name(file_name):
    """
    处理文件的名字
    比如xx.jpg--->y798hi3rkkjuiiuh3i8shdf.jpg
    """
    return str(uuid.uuid1()) + os.path.splitext(file_name)[-1]


def check_login(func):
    """登录验证"""

    @wraps(func)
    def inner(*args, **kwargs):
        if not args[0].session.get("logined"):
            return redirect("/bg/login")
        else:
            return func(*args, **kwargs)

    return inner


if __name__ == '__main__':
    print(encryption_md5("12345"))  # e10adc3949ba59abbe56e057f20f883e
    print(do_file_name("xx.jpg"))